//////////////////////////////////////////////////////////////////////
// <copyright file=CubicPolynomial.cs">
//     Copyright (c) 2010
//     Released under  GNU GENERAL PUBLIC LICENSE
//     http://dev.perl.org/licenses/gpl1.html
// </copyright>
// <date> 3/3/2010 12:33:35 AM </date>
// <author> B. Endrovski </author>
// <web> http://furiouspixels.blogspot.com/ </web>
//////////////////////////////////////////////////////////////////////

using System;
using System.Collections.Generic;
using System.Text;

namespace Curves {

    /// <summary>
    /// Most curves use cubic polynomials exclusively, and this is a simple class implemeting one.
    /// </summary>
    public class CubicPolynomial {
        protected float a, b, c, d;

		/// <summary>
		/// Creates the a*x^3 + b*x^2 + c*x + d polynomial
		/// </summary>		
        public CubicPolynomial(float a, float b, float c, float d) {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }

		/// <summary>
		/// Calculates the value using the horner rule
		/// </summary>
		/// <returns>The value ot the polynomial at x</returns>
        public float Value(float x) {
            float result = a;
            result = result * x + b;
            result = result * x + c;
            result = result * x + d;
            return result;
        }

		/// <summary>
		/// Calculates the value of the first derivate using the horner rule
		/// </summary>
        public float FirstDerivate(float x) {
            float result = 3.0f * a;
            result = result * x + 2.0f * b;
            result = result * x + c;
            return result;
        }

		/// <summary>
		/// Calculates the value of the second derivate using the horner rule
		/// </summary>
        public float SecondDerivate(float x) {
            float result = 6.0f * a;
            result = result * x + 2.0f * b;           
            return result;
        }
    }
}